<?php
class Arm_Db_Table_ManyToMany extends Arm_Db_Table_Abstract{

    protected $enabled  = null;
    protected $disabled = null;

    protected $parent_object = null;
    protected $dependent     = null;
   
    public function setParentId($id){

        $filter = new Zend_Filter_Int();
        $id     = $filter->filter($id);

        $table  = new $this->_referenceMap['parent']['refTableClass'];
        $object = $table->find($id)->current();

        if($object){
            $this->parent_object = $object;
        }else{
            throw new Zend_Exception('Parent não encontrado com o id: ' . $id);
        }
    }

    public function getParent(){
        return $this->parent_object;
    }

    public function getEnabled(){
       
        if(!is_null($this->enabled)) return $this->enabled;

        $select = $this->select();
        $select->where($this->_referenceMap['parent']['columns'] . ' = ?',$this->parent_object->key(),'integer');

        $this->enabled = $this->fetchAll($select);

        return $this->enabled;
    }

    public function getDependentDisabled(){
        
        if(!is_null($this->disabled)) return $this->disabled;

        // monta not in
        $i = 0;
        $notIn = '(';
        $column = $this->_referenceMap['dependent']['columns'];
        foreach($this->getEnabled() as $row){
            if($i) $notIn .= ', ';
            $notIn .= $row->$column;
            $i++;
        }
        $notIn .= ')';
        
        $table  = new $this->_referenceMap['dependent']['refTableClass'];
        $select = $table->select();
        if($notIn != '()') $select->where($this->_referenceMap['dependent']['refColumns'] . ' not in ' . $notIn);
        $this->disabled = $table->fetchAll($select);

        return $this->disabled;
    }

    public function setDependent($dependet_id){
        
        $this->dependent = $dependet_id;
    }

    public function rmDependent($dependent_id){
    
        $select = $this->select();
        $select->where($this->_referenceMap['dependent']['columns'] . ' = ?',$dependent_id);
        $select->where($this->_referenceMap['parent']['columns'] . ' = ?',$this->parent_object->id);

        $object = $this->fetchAll($select)->current();
        $object->delete();
    }

    public function createRow(array $data = array(), $defaultSource = null){
        
        if(is_null($this->dependent) or is_null($this->parent_object)) return parent::createRow($data, $defaultSource);

        $select = $this->select();
        $select->where($this->_referenceMap['parent']['columns'] . ' = ?',$this->parent_object->key(),'integer');
        $select->where($this->_referenceMap['dependent']['columns'] . ' = ?',$this->dependent,'integer');
        $row = $this->fetchAll($select)->current();
        if($row) return $row;

        $row = parent::createRow($data, $defaultSource);
        $dependent_field = $this->_referenceMap['dependent']['columns'];
        $parent_field    = $this->_referenceMap['parent']['columns']; 
        $row->$dependent_field  = $this->dependent;
        $row->$parent_field     = $this->parent_object->id;

        if(isset($row->system_id)){
            $session = new Zend_Session_Namespace();
            $row->system_id = $session->system->id;
        }
        return $row;
    }
}
